package archforce.atp.x.bs;

import archforce.atp.x.ssh.tool.PersistenceConfigTool;
import archforce.atp.x.ui.frame.component.FacedComponent;
import archforce.atp.x.ui.impl.common.model.PanelDataModel;
import archforce.atp.x.ui.impl.left.IPPortGroupComponent;
import archforce.atp.x.ui.impl.left.IPPortModel;
import archforce.atp.x.ui.impl.right.IPPortMappingItemComponent;
import archforce.atp.x.ui.impl.right.IPPortMappingModel;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wx
 * @version 1.0
 * @date 2022/4/28 15:07
 */
public class SaveBtnBS {

    /**
     *
     * @param customerJumpConfigPanel 链接配置自定义模式面板
     * @param staticModelComponent 固定模式面板
     * @param selectedModel 右侧映射配置选中的模式的面板
     * @param key
     * @param autoModel 右侧面板是否自动匹配模式
     * @param user 容器平台用户名
     * @param password 容器平台密码
     */
    public void save(FacedComponent<IPPortGroupComponent, List<IPPortModel>> customerJumpConfigPanel,//链接配置自定义模式面板
                     FacedComponent<IPPortMappingItemComponent, IPPortMappingModel> staticModelComponent,//固定模式面板
                     Map<String,FacedComponent> selectedModel,//右侧映射配置选中的模式的面板
                     String key,
                     Boolean autoModel,
                     String user,
                     String password){
        List<List<IPPortModel>> jumpConfig = customerJumpConfigPanel.getData();
        Map<String,List<IPPortMappingModel>> mappingConfig = new HashMap<>();
        selectedModel.entrySet().forEach(entry->{
            mappingConfig.put(entry.getKey(), entry.getValue().getData());
        });
        PanelDataModel wholeData = new PanelDataModel();
        wholeData.setConnectConfigData(jumpConfig);
        wholeData.setMappingConfigData(mappingConfig);
        wholeData.setDefaultMappingConfigData(staticModelComponent.getData());
        wholeData.setConnectConfigSelectedIndex(customerJumpConfigPanel.getSelectedID());
        wholeData.setAutoModel(autoModel);
        wholeData.setUser(user);
        wholeData.setPassword(password);
        try {
            PersistenceConfigTool.persistencePanelData(wholeData);
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
    }
}
